package com.budwk.app.sqfwV2.generator.entrepreneurShipHouse;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.component.DictComponent;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.EntrepreneurShipHouseComponent;
import com.budwk.app.sqfwV2.dto.dto.EntrepreneurShipHouseParamDTO;
import com.budwk.app.sqfwV2.dto.image.ExcelProblemImageConfigBO;
import com.budwk.app.sqfwV2.dto.vo.JobPersonInfoVo;
import com.budwk.app.sqfwV2.enums.EntrepreneurHouseFileEnum;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创业实体入户 EntrepreneurShipHouse
 * 带动就业情况表.xlsx
 */

@Slf4j
public class DrivingEmploymentSituationExcel extends BaseTemplateExcelGenerator {

    // 表头信息
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {

    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        log.info("DrivingEmploymentSituationExcel customerDataParam summaryList:  {}", JSONObject.toJSONString(summaryList));
        int num = 1;
        for (Sqfw_formSummary_new summary : summaryList) {
            EntrepreneurShipHouseParamDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), EntrepreneurShipHouseParamDTO.class);
            log.info("DrivingEmploymentSituationExcel customerDataParam roadPdfDTO: {}", JSONObject.toJSONString(roadPdfDTO));
            if (!isGenerateExcel(roadPdfDTO)) {
                continue;
            }
            // 1 构建法人信息
            Map<String, Object> map = buildBaseMap(roadPdfDTO);
            map.put("num", num ++);
            listMap.add(map);
            // 2 构建带动就业人员信息
            List<JobPersonInfoVo> jobPersonInfo = roadPdfDTO.getJobPersonInfo();
            for (JobPersonInfoVo param : jobPersonInfo) {
                Map<String, Object> map1 = buildJobMap(param, roadPdfDTO);
                map1.put("num", num ++);
                listMap.add(map1);
            }
        }
        log.info("DrivingEmploymentSituationExcel customerDataParam end: {}", JSONObject.toJSONString(listMap));
    }

    private Map<String, Object> buildJobMap(JobPersonInfoVo param, EntrepreneurShipHouseParamDTO roadPdfDTO) {
        Map<String, Object> map = new HashMap<>();
        map.put("placeName", EntrepreneurShipHouseComponent.formatStr(roadPdfDTO.getPlaceName()));
        map.put("operator", EntrepreneurShipHouseComponent.formatStr(param.getName()));
        try {
            map.put("gender", "");
            map.put("age", EntrepreneurShipHouseComponent.NO_SUBMIT);
            Integer age = EntrepreneurShipHouseComponent.calAge(param.getIdcard(), param.getAge());
            if (age != null) {
                map.put("age", age);
            }
            Integer gender = EntrepreneurShipHouseComponent.calGender(param.getIdcard(), param.getGender());
            if (gender != null) {
                map.put("gender", 1 == gender ? "男" : "女");
            }
        } catch (Exception e) {
            map.put("gender", "");
            map.put("age", EntrepreneurShipHouseComponent.NO_SUBMIT);
        }
        map.put("corporateIDNumber", EntrepreneurShipHouseComponent.formatStr(param.getIdcard()));
        map.put("nation", EntrepreneurShipHouseComponent.formatStr(param.getNation()));
        map.put("educationalLevel", EntrepreneurShipHouseComponent.formatStr(param.getEducationalLevel()));
        map.put("graduationTime", EntrepreneurShipHouseComponent.NO_SUBMIT);
        if (param.getGraduationTime() != null) {
            map.put("graduationTime", LocalDateTimeUtil.format(param.getGraduationTime(), DatePattern.CHINESE_DATE_PATTERN));
        }
        map.put("graduateSchool", EntrepreneurShipHouseComponent.formatStr(param.getGraduateSchool()));
        map.put("classMajor", EntrepreneurShipHouseComponent.formatStr(param.getClassMajor()));
        map.put("houseRegistProperty", EntrepreneurShipHouseComponent.formatStr(param.getHouseRegistProperty()));
        map.put("houseRegistSituation", EntrepreneurShipHouseComponent.formatStr(param.getHouseRegistSituation()));
        map.put("householdAddress", EntrepreneurShipHouseComponent.formatStr(param.getHouseholdAddress()));
        map.put("permanentAddress", EntrepreneurShipHouseComponent.formatStr(param.getPermanentAddress()));
        map.put("employmentTime", LocalDateTimeUtil.format(param.getEmploymentTime(), DatePattern.CHINESE_DATE_PATTERN));
        map.put("operatorTelephone", EntrepreneurShipHouseComponent.formatStr(param.getPhone()));
        map.put("monthSalary", EntrepreneurShipHouseComponent.formatStr(param.getMonthSalary()));
        map.put("engageType", EntrepreneurShipHouseComponent.formatStr(param.getEngageType())); // 从事工种
        map.put("engageTypeengageType", EntrepreneurShipHouseComponent.formatStr(param.getEngageType()));
        map.put("remark", "");
        return map;
    }

    private Map<String, Object> buildBaseMap(EntrepreneurShipHouseParamDTO param) {
        Map<String, Object> map = new HashMap<>();
        map.put("placeName", EntrepreneurShipHouseComponent.formatStr(param.getPlaceName()));
        map.put("operator", EntrepreneurShipHouseComponent.formatStr(param.getOperator()));
        map.put("gender", "");
        if (param.getGender() != null) {
            map.put("gender", 1 == param.getGender() ? "男" : "女");
        }
        map.put("age", EntrepreneurShipHouseComponent.NO_SUBMIT);
        if (param.getAge() != null) {
            map.put("age", param.getAge());
        }
        map.put("corporateIDNumber", EntrepreneurShipHouseComponent.formatStr(param.getCorporateIDNumber()));
        map.put("nation", EntrepreneurShipHouseComponent.formatStr(param.getNation()));
        map.put("educationalLevel", EntrepreneurShipHouseComponent.formatStr(param.getEducationalLevel()));
        map.put("graduationTime", EntrepreneurShipHouseComponent.NO_SUBMIT);
        if (param.getGraduationTime() != null) {
            map.put("graduationTime", LocalDateTimeUtil.format(param.getGraduationTime(), DatePattern.CHINESE_DATE_PATTERN));
        }
        map.put("graduateSchool", EntrepreneurShipHouseComponent.formatStr(param.getGraduateSchool()));
        map.put("classMajor", EntrepreneurShipHouseComponent.formatStr(param.getClassMajor()));
        map.put("houseRegistProperty", EntrepreneurShipHouseComponent.formatStr(param.getHouseRegistProperty()));
        map.put("houseRegistSituation", EntrepreneurShipHouseComponent.formatStr(param.getHouseRegistSituation()));
        map.put("householdAddress", EntrepreneurShipHouseComponent.formatStr(param.getHouseholdAddress()));
        map.put("permanentAddress", EntrepreneurShipHouseComponent.formatStr(param.getPermanentAddress()));
        map.put("employmentTime", LocalDateTimeUtil.format(param.getEmploymentTime(), DatePattern.CHINESE_DATE_PATTERN));
        map.put("operatorTelephone", EntrepreneurShipHouseComponent.formatStr(param.getOperatorTelephone()));
        map.put("monthSalary", EntrepreneurShipHouseComponent.formatStr(param.getMonthSalary()));
        map.put("engageType", EntrepreneurShipHouseComponent.formatStr(param.getEngageType())); // 从事工种
        map.put("engageTypeengageType", EntrepreneurShipHouseComponent.formatStr(param.getEngageType()));
        map.put("remark", "");
        return map;

    }

    private boolean isGenerateExcel(EntrepreneurShipHouseParamDTO roadPdfDTO) {

        return true;
    }

    @Override
    protected String getTemplateName() {
        return EntrepreneurHouseFileEnum.ONE_EXCEL.getFilePath();
    }

    @Override
    public String getDownloadFileName() {
        return DictComponent.getDictName(EntrepreneurHouseFileEnum.ONE_EXCEL.getCode(), null);
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return ExcelProblemImageConfigBO.class;
    }
}
